<template>
  <div class="icon-picker">
    <!-- 触发输入框 - 增加更明显的选中状态 -->
    <el-input
      v-model="displayValue"
      placeholder="请选择图标"
      readonly
      class="icon-trigger"
      :class="{ 'has-selection': selectedIconName }"
      @click="showDialog = true"
    >
      <template #prefix>
        <el-icon v-if="selectedIconName" class="selected-icon-preview">
          <component :is="selectedIconName" />
        </el-icon>
      </template>
      <template #append>
        <el-button @click="showDialog = true">
          <el-icon><Search /></el-icon>
        </el-button>
      </template>
    </el-input>

    <!-- 图标选择弹窗 -->
    <el-dialog
      v-model="showDialog"
      title="选择图标"
      width="600px"
      append-to-body
    >
      <div class="icon-picker-dialog">
        <!-- 搜索框 -->
        <div class="search-container">
          <el-input
            v-model="searchText"
            placeholder="搜索图标"
            clearable
            prefix-icon="Search"
            @input="tempSelectedIcon = ''"
          />
        </div>

        <!-- 图标网格 - 增强选中状态 -->
        <div class="icon-grid">
          <div
            v-for="icon in filteredIcons"
            :key="icon.name"
            class="icon-item"
            :class="{
              'icon-selected': selectedIconName === icon.name,
              'icon-temp-selected': tempSelectedIcon === icon.name
            }"
            @click="handleSelectIcon(icon.name)"
          >
            <div class="icon-badge" v-if="selectedIconName === icon.name">
              <el-icon><Select /></el-icon>
            </div>
            <el-icon :size="24">
              <component :is="icon.component" />
            </el-icon>
            <span class="icon-name">{{ icon.name }}</span>
          </div>
        </div>
      </div>

      <template #footer>
        <div class="selection-preview" v-if="tempSelectedIcon">
          当前选择: 
          <el-icon :size="20">
            <component :is="tempSelectedIcon" />
          </el-icon>
          <span class="preview-name">{{ tempSelectedIcon }}</span>
        </div>
        <div class="dialog-footer">
          <el-button @click="cancelSelection">取消</el-button>
          <el-button 
            type="primary" 
            @click="confirmSelection"
            :disabled="!tempSelectedIcon"
          >
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { Search, Select } from '@element-plus/icons-vue'
import { elementPlusIcons } from '@/utils/iconLibrary'

const props = defineProps({
  modelValue: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['update:modelValue'])

const showDialog = ref(false)
const searchText = ref('')
const tempSelectedIcon = ref('')

const selectedIconName = computed({
  get: () => props.modelValue,
  set: (val) => emit('update:modelValue', val)
})

const displayValue = computed(() => 
  selectedIconName.value || '请选择图标'
)

const filteredIcons = computed(() => {
  return elementPlusIcons.filter(icon =>
    icon.name.toLowerCase().includes(searchText.value.toLowerCase())
  )
})

const handleSelectIcon = (iconName) => {
  tempSelectedIcon.value = iconName
}

const confirmSelection = () => {
  if (tempSelectedIcon.value) {
    selectedIconName.value = tempSelectedIcon.value
  }
  showDialog.value = false
}

const cancelSelection = () => {
  tempSelectedIcon.value = ''
  showDialog.value = false
}

// 打开弹窗时初始化临时选择
watch(showDialog, (val) => {
  if (val) {
    tempSelectedIcon.value = selectedIconName.value
  }
})
</script>

<style scoped>
.icon-picker {
  width: 100%;
}

/* 触发输入框样式 */
.icon-trigger {
  cursor: pointer;
}

.icon-trigger.has-selection :deep(.el-input__inner) {
  padding-left: 40px;
  color: var(--el-color-primary);
  font-weight: 500;
}

.selected-icon-preview {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--el-color-primary);
}

/* 图标选择弹窗 */
.icon-picker-dialog {
  padding: 10px;
}

.search-container {
  margin-bottom: 15px;
}

/* 图标网格布局 */
.icon-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 15px;
  max-height: 400px;
  overflow-y: auto;
  padding: 5px;
}

.icon-item {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 15px 10px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
  border: 1px solid var(--el-border-color);
}

.icon-item:hover {
  border-color: var(--el-color-primary-light-5);
  box-shadow: 0 2px 8px var(--el-color-primary-light-8);
}

.icon-selected {
  border-color: var(--el-color-primary);
  background-color: var(--el-color-primary-light-9);
}

.icon-temp-selected {
  border-color: var(--el-color-primary);
  background-color: var(--el-color-primary-light-8);
}

.icon-badge {
  position: absolute;
  top: -8px;
  right: -8px;
  background: var(--el-color-primary);
  color: white;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
}

.icon-name {
  margin-top: 8px;
  font-size: 12px;
  text-align: center;
  word-break: break-all;
}

/* 底部预览区域 */
.selection-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-right: auto;
  color: var(--el-color-primary);
  font-weight: 500;
}

.preview-name {
  margin-left: 4px;
}

.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  width: 100%;
}
</style>